﻿using Ci.Presentation.Web.Models;
using CI.Domain.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using TransDepot.Presentation.Web.Extensions;

namespace CI.Presentation.Web.Controllers
{
    public class ConsultaController : Controller
    {
        [HttpGet]
        public ActionResult Index()
        {
            return PartialView();
        }

        [HttpGet]
        public ActionResult SearchConsulta(string dni, string nombres)
        {
            DomainModelContext dataContext = new DomainModelContext();

            var consultas = from p in dataContext.vwPacienteConsulta
                            where p.numDocumento == dni.Trim() //|| p.nombres.Contains(nombres.Trim())
                            select p;

            return Json(consultas.ToList(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetConsulta(string dni, int idConsulta)
        {
            DomainModelContext dataContext = new DomainModelContext();

            var consulta = from p in dataContext.vwPacienteConsulta
                           where p.numDocumento == dni.Trim() && p.idConsultaDental == idConsulta
                           select p;

            return Json(consulta.SingleOrDefault(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetMaxConsulta()
        {
            int max = 0;

            DomainModelContext dataContext = new DomainModelContext();

            /*try
            {*/
            max = (from p in dataContext.Consulta
                   select p.idConsultaDental).Max();

            /*           }
                       catch
                       {
                           max = 0;
                       }*/
            return Json(max + 1, JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        [ForceModelParse(typeof(ConsultaVM))]
        public bool Save(ConsultaVM model)
        {
            try
            {
                DomainModelContext dataContext = new DomainModelContext();
                Consulta consulta = new Consulta()
                {
                    idConsultaDental = model.idConsultaDental,
                    fechaCreacion = model.fechaCreacion,
                    motivoConsulta = model.motivoConsulta,
                    sintomas = model.sintomas,
                    observacion = model.observacion,
                    idCodigoPaciente = model.idCodigoPaciente
                };

                dataContext.Consulta.InsertOnSubmit(consulta);
                dataContext.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }

        [HttpPost]
        [ForceModelParse(typeof(ConsultaVM))]
        public bool Edit(ConsultaVM model, int idConsulta)
        {
            try
            {
                DomainModelContext dataContext = new DomainModelContext();
                
                var c = (from p in dataContext.vwPacienteConsulta
                         where p.idConsultaDental == idConsulta
                        select p).SingleOrDefault();

                Consulta consulta = new Consulta()
                {
                    idConsultaDental = c.idConsultaDental,
                    fechaCreacion = c.fechaCreacion,
                    motivoConsulta = c.motivoConsulta,
                    sintomas = c.sintomas,
                    observacion = c.observacion,
                    idCodigoPaciente = c.idCodigoPaciente
                };

                dataContext.Consulta.InsertOnSubmit(consulta);
                dataContext.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }        
    }
}
